/**
 * 
 */
package com.hw.hcit.feature;

import java.io.FileOutputStream;
import java.io.IOException;

import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.os.IPowerManager;
import android.os.SystemProperties;
import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings;
/**
 * @author gaoshengpan
 *
 */
public class BackLightTestItem extends JTestItem implements OnClickListener {
   private int i = 0;
   private RelativeLayout layout;
   private int brightness;
   private static int sysBrightness;
   private PowerManager pm;
   private ContentResolver cr;
   private String TAG = "BackLightTestItem";
   private static final byte[] LIGHTE_OFF = { '0' };
   private static final byte[] LIGHTE_MIN = { '3', '0' };
   private static final byte[] LIGHTE_DEF = { '1', '0', '2' };
   private static final byte[] LIGHTE_MAX = { '2', '5', '5' };
   private boolean mIsFinish = false;
   private RelativeLayout relativeLayout, relativeLayout2;
   private Button mButton1, mButton2;
   private TextView tvTextView;

   /**
    * @param cb
    */
   public BackLightTestItem(JCtrlCallback cb) {
      super(cb);
      // TODO Auto-generated constructor stub
   }

   /**
    * 
    */
   public BackLightTestItem() {
      // TODO Auto-generated constructor stub
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
   }

   /**
    * 
    */
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.backlight_test, container,
            false);
      layout = (RelativeLayout) rootView.findViewById(R.id.backlight);
      relativeLayout = (RelativeLayout) rootView.findViewById(R.id.confirm);
      relativeLayout2 = (RelativeLayout) rootView
            .findViewById(R.id.backlight);
      tvTextView = (TextView) rootView.findViewById(R.id.test_title);
      tvTextView.setText(getString(R.string.backlight_test));
      mButton1 = (Button) rootView.findViewById(R.id.button1);
      mButton2 = (Button) rootView.findViewById(R.id.button2);
      mButton1.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (Config.CURR_PROJECT.isAuto()) {
               if (!Config.ageingTested) {
                  rewriteOneByteOfNv2499(4, 'P');

               } else {
                  rewriteOneByteOfNv2499(35, 'P');
               }
            }
            testFinish(true, 1, true);

         }
      });
      mButton2.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (Config.CURR_PROJECT.isAuto()) {
               if (!Config.ageingTested) {
                  rewriteOneByteOfNv2499(4, 'F');

               } else {
                  rewriteOneByteOfNv2499(35, 'F');
               }
            }
            testFinish(false, 1, false);

         }
      });
      return rootView;
   }

   @Override
   public void onClick(View arg0) {
      // TODO Auto-generated method stub

   }

   Handler mHandler = new Handler() {

      public void handleMessage(Message msg) {
         super.handleMessage(msg);
         try {
            PowerManager power1 = (PowerManager) getActivity()
                  .getSystemService(Context.POWER_SERVICE);
            Log.d(TAG, "pm.getMinimumScreenBrightnessSetting()-------->1"
                  + power1.getMinimumScreenBrightnessSetting());
            Log.d(TAG, "pm.getDefaultScreenBrightnessSetting()-------->2"
                  + power1.getDefaultScreenBrightnessSetting());
            Log.d(TAG, "pm.getMaximumScreenBrightnessSetting()-------->3"
                  + power1.getMaximumScreenBrightnessSetting());
            switch (msg.what) {
            case 0:
               Log.d(TAG,
                     "pm.getMinimumScreenBrightnessSetting()-------->0");
               setBackLight(LIGHTE_OFF);
               mHandler.sendEmptyMessageDelayed(1, 400);
               break;
            case 1:
               Log.d(TAG,
                     "pm.getMinimumScreenBrightnessSetting()-------->1");
               setBackLight(LIGHTE_MIN);
               mHandler.sendEmptyMessageDelayed(2, 400);
               break;
            case 2:
               Log.d(TAG,
                     "pm.getMinimumScreenBrightnessSetting()-------->2");
               setBackLight(LIGHTE_DEF);
               mHandler.sendEmptyMessageDelayed(3, 400);
               break;
            case 3:
               Log.d(TAG,
                     "pm.getMinimumScreenBrightnessSetting()-------->3");
               setBackLight(LIGHTE_MAX);
               i++;
               if (i != 3) {
                  Log.d(TAG,
                        "i--------------------------------------------->"
                              + i);
                  mHandler.sendEmptyMessageDelayed(0, 400);
               } else {
                  relativeLayout.setVisibility(View.VISIBLE);
                  relativeLayout2.setBackgroundColor(Color.DKGRAY);
               }

               break;

            default:
               break;
            }

         } catch (Exception e) {
            Log.d(TAG, "toggleBrightness: " + e);
         }
      }
   };

   private void setBackLight(byte[] i) {
      FileOutputStream red;
      try {
         byte[] lightData = LIGHTE_OFF;
         lightData = i;
         Log.d(TAG, "setBackLight-------->" + lightData.toString());
         red = new FileOutputStream(
               "/sys/class/leds/lcd-backlight/brightness");
         red.write(lightData);
         red.close();
      } catch (IOException e) {
         e.printStackTrace();
      }

   }

   /*
    * (non-Javadoc)
    * 
    * @see android.app.Fragment#onResume()
    */
   @Override
   public void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      i = 0;
      try {
          brightness = Settings.System.getInt(Config.GetActivity()
                .getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
          Log.d(TAG, "brightness-onresume1-->" + brightness);
          Settings.System.putInt(Config.GetActivity().getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
          Log.d(TAG,
                "brightnessMode-onResume-->"
                      + Settings.System.getInt(Config.GetActivity()
                            .getContentResolver(),
                            Settings.System.SCREEN_BRIGHTNESS_MODE));
       } catch (Exception e) {
          // TODO: handle exception
          e.printStackTrace();
       }
      mHandler.sendEmptyMessage(0);
   }

   @Override
   public void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      mIsFinish = true;
     /* Settings.System.putInt(Config.GetActivity().getContentResolver(),
              Settings.System.SCREEN_BRIGHTNESS_MODE,
              Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);*/
        Settings.System.putInt(Config.GetActivity().getContentResolver(),
              Settings.System.SCREEN_BRIGHTNESS, brightness);
        try {
           Log.d(TAG,
                 "brightnessMode-onpause-->"
                       + Settings.System.getInt(Config.GetActivity()
                             .getContentResolver(),
                             Settings.System.SCREEN_BRIGHTNESS_MODE));
           Log.d(TAG,
                 "brightness-onpause-->"
                       + Settings.System.getInt(Config.GetActivity()
                             .getContentResolver(),
                             Settings.System.SCREEN_BRIGHTNESS));
        } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
        }
      mHandler.removeMessages(0);
      mHandler.removeMessages(1);
      mHandler.removeMessages(2);
      mHandler.removeMessages(3);
   }

   @Override
   public void onDestroy() {
      // TODO Auto-generated method stub

      super.onDestroy();
   }

}
